package com.nit.tools.doc;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import freemarker.template.TemplateException;

import java.io.*;
import java.nio.charset.Charset;

public class Itext5HtmlToPDF {

    public static void main(String[] args) throws IOException, TemplateException {
        new Itext5HtmlToPDF().createPdf();


//String html ="D:\\doc_test\\";
//m.put("title","特殊困难老人家庭适老化改造验收表");
//Map m = new HashMap();
//
//List tdList = Lists.newArrayList();
//Tdvo a  = new Tdvo();
//a.setA("aaaaaaaaaaaaa");
//a.setB("bbbbbbbbbbbbb");
//tdList.add(a);
//
//
//for(int i=0;i<30;i++){
//	Tdvo b  = new Tdvo();
//	b.setA("ccccc"+i);
//	b.setB("ddddd");
//	tdList.add(b);
//}
//
//
//m.put("tdList",tdList);
////m.put("tdSize",100);
//
//HtmlVo vo = new HtmlVo();
//vo.setTdList(tdList);
//vo.setTitle("特殊困难老人家庭适老化改造验收表");
//vo.setTdSize(Long.valueOf(tdList.size()+2));
//
//String tmpHtml =  GuoFtlUtil.ftlToString(html,"index.ftl",vo);
//new Itext5HtmlToPDF().createPdfFreemark(tmpHtml);
    }


    public void createPdf() {
        try {
            String pdfDest = "D:\\doc\\doc_test\\aaa.pdf";
            String indexhtml = "D:\\doc\\doc_test\\index_v7.html";

//	String pdfDest ="/opt/aaa.pdf";
//	String indexhtml ="/opt/index_v6.html";

//	BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
            BaseFont bfChinese = BaseFont.createFont("C:\\WINDOWS\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, true);
//	BaseFont bfChinese = BaseFont.createFont("/usr/share/fonts/Fonts/simsunb.ttf", BaseFont.IDENTITY_H, true);
//	BaseFont bfChinese = BaseFont.createFont("/opt/SimSun.ttf", BaseFont.IDENTITY_H, true);

            Font font = new Font(bfChinese, 20, Font.BOLD);

//	Document document = new Document(PageSize.A4, 10, 10, 10, 10);
            Document document = new Document(PageSize.A4);

            PdfWriter mPdfWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfDest));
            document.open();
            Paragraph title = new Paragraph("特殊困难老人家庭适老化改造验收表", font);
            title.setAlignment(Element.ALIGN_CENTER);
            title.setFont(font);

            document.add(title);

//	String s = getHtml();
            String s = aaaaa(indexhtml);
            System.out.print(s);
            ByteArrayInputStream bin = new ByteArrayInputStream(s.getBytes());
            XMLWorkerHelper.getInstance().parseXHtml(mPdfWriter, document, bin, Charset.forName("UTF-8"), new ChinaFontProvide());
            document.close();
            mPdfWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void createPdfFreemark(String tmpHtml) {
        try {
            String pdfDest = "D:\\doc_test\\aaa.pdf";
            String indexhtml = "D:\\doc_test\\index.html";

            BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
            Font font = new Font(bfChinese, 20, Font.NORMAL);

//	Document document = new Document(PageSize.A4, 10, 10, 10, 10);
            Document document = new Document(PageSize.A4);

            PdfWriter mPdfWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfDest));
            document.open();
//	Paragraph title = new Paragraph("特殊困难老人家庭适老化改造验收表", font);
//	title.setAlignment(Element.ALIGN_CENTER);
//
//	document.add(title);

//	String s = aaaaa(indexhtml);
//	System.out.print( s);
            ByteArrayInputStream bin = new ByteArrayInputStream(tmpHtml.getBytes());
            XMLWorkerHelper.getInstance().parseXHtml(mPdfWriter, document, bin, Charset.forName("UTF-8"), new ChinaFontProvide());
            document.close();
            mPdfWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String aaaaa(String args) {
        try {
            // 创建一个File对象，指定要读取的文件路径
            File file = new File(args);

            // 创建一个BufferedReader对象，将其与FileInputStream关联起来
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

            String reslt = "";
            String line;
            while ((line = reader.readLine()) != null) {
                reslt += line;
            }

            // 关闭reader
            reader.close();

            return reslt;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getHtml() {

        StringBuffer html = new StringBuffer();
        html.append("<div style='color:green;font-size:20px;'>你好世界！hello world !</div>");
        html.append("<span style='color:red'>what are you 弄啥咧!</span>");
        html.append("<h1>标题</h1>");
        html.append("<table border='1px solid black'>");
        html.append("<tr>");
        html.append("<th>序号</th>");
        html.append("<th>用户名</th>");
        html.append("<th>性别</th>");
        html.append("</tr>");
        html.append("<tr>");
        html.append("<td>1</td>");
        html.append("<td>fengxing</td>");
        html.append("<td>男</td>");
        html.append("</tr>");
        html.append("<tr>");
        html.append("<td>2</td>");
        html.append("<td>admin</td>");
        html.append("<td>女</td>");
        html.append("</tr>");
        html.append("</table>");
        return html.toString();
    }

    /**
     * 提供中文
     */
    public static final class ChinaFontProvide implements FontProvider {

        @Override
        public Font getFont(String arg0, String arg1, boolean arg2, float arg3,
                            int arg4, BaseColor arg5) {
            BaseFont bfChinese = null;
            try {
                bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                        BaseFont.NOT_EMBEDDED);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Font FontChinese = new Font(bfChinese, 10, Font.NORMAL);
            return FontChinese;
        }

        @Override
        public boolean isRegistered(String arg0) {
            return false;
        }
    }
}